package engine.logic.walk
{
	import engine.GameData;
	import engine.scene.SceneChangeManager;
	import engine.scene.SceneManager;
	
	import flash.geom.Point;
	import flash.net.SharedObject;
	
	import newx.StageInfo;
	
	
	/**
	 *	滚轴移动（玩家自己移动） 
	 * @author lw
	 * 
	 */
	public class RollerMoveLogic extends NormalMoveLogic
	{
		private var _sceneModel	: ISceneModel;
		//移动类型 （0边界切换/滚轴1）
		public static var MoveType: int = 1;
		
		/**
		 * 移动到的坐标
		 * @param x
		 * @param y
		 */
		override protected function move( xDistance : int, yDistance : int ):void
		{
			//保存当前坐标
			_curePoint = new Point(_curePoint.x + xDistance, _curePoint.y + yDistance);
			
			// -1/1/   没有/垂直居中
			var playerResult 	: int;
			
			if(1 == MoveType)
			{
				playerResult = playerCenter( _moveModel.point.x );
				if( -1 == playerResult )
				{//人物不居中，人物移动
					_moveModel.move( xDistance, yDistance );
					NpcIconManage.getInstance().playMoveCheck(_moveModel.point.x, _moveModel.point.y);
					return;
				}
				_moveModel.move( xDistance, yDistance );
				_sceneModel.move( xDistance, yDistance );
				NpcIconManage.getInstance().playMoveCheck(_moveModel.point.x, _moveModel.point.y);
			}else
			{
				playerResult = playerBorder( _moveModel.point.x );
				if(-1 == playerResult)
				{//人物不居中，人物移动
					_moveModel.move( xDistance, yDistance );
					NpcIconManage.getInstance().playMoveCheck(_moveModel.point.x, _moveModel.point.y);
					return;
				}
				_moveModel.move( xDistance, yDistance );
				_sceneModel.moveFollow( xDistance, playerResult );
				NpcIconManage.getInstance().playMoveCheck(_moveModel.point.x, _moveModel.point.y);
			}
		}
		
		
		
		/**
		 *	玩家是否居中 
		 * @return -1/1 没有/垂直居中
		 */
		private function playerCenter( xRole : int ):int
		{
			var centerX		: int	= StageInfo.stage.stageWidth * .5;
			
			var xRole 		: int	= SceneManager.getInstance().norSceneView.localToGlobal(new Point(xRole, 0)).x;
			if(xRole > centerX - 25 && xRole < centerX + 25)
			{
				return 1;
			}
			
			return -1;
		}
		
		private function playerBorder( xRole : int ):int
		{
			var borderX		: int	= SceneChangeManager.getGameWidth();
		
			var xRole 		: int	= SceneManager.getInstance().norSceneView.localToGlobal(new Point(xRole, 0)).x;
			
			if(xRole < 200)
			{
				return 1;
			}
			
			if(xRole > borderX - 200)
			{
				return 2;
			}
			
			return -1;
		}
		public function set sceneModel(value:ISceneModel):void
		{
			_sceneModel = value;
		}
	}
}